package com.tommy.jiami.ui.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.tommy.jiami.R;

public abstract class BaseDialogFragment extends DialogFragment implements OnClickListener
{

	protected Button mPositiveButton,mNegativeButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view=getContentView(inflater, container, savedInstanceState);
		if(view instanceof LinearLayout){
			LinearLayout layout=(LinearLayout)view;
			View buttons=getActivity().getLayoutInflater().inflate(R.layout.dialog_buttons,null);
			mPositiveButton=(Button)buttons.findViewById(R.id.dialog_buttons_positive);
			mPositiveButton.setOnClickListener(this);
			mNegativeButton=(Button)buttons.findViewById(R.id.dialog_buttons_negative);
			mNegativeButton.setOnClickListener(this);
			layout.addView(buttons);
		}
		return view;
	}
	
	protected abstract View getContentView(LayoutInflater flater,ViewGroup container,Bundle saveInstanceState);
	
	protected void onPositiveButtonClick(){
		getDialog().cancel();
	}
	
	protected void onNegativeButtonClick(){
		getDialog().cancel();
	}

	@Override
	public void onClick(View view)
	{
		switch(view.getId()){
			case R.id.dialog_buttons_positive:
				onPositiveButtonClick();
				break;
			case R.id.dialog_buttons_negative:
				onNegativeButtonClick();
				break;
		}
	}	
	
}
